/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scm.hibernate.entities;

import com.scm.hibernate.utilities.HibernateUtil;
import java.lang.reflect.Field;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author YESHA
 */
public class AbstractEntity {

    public Object add() {
        Object flag = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.save(this);
            tx.commit();
            flag = this;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean delete(Long id) {
        boolean flag = false;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            Object obj = session.get(this.getClass(), id);
            session.delete(obj);
            tx.commit();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean update(Long id) {
        boolean flag = false;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            Object obj = session.get(this.getClass(), id);
            Field updated_fields[] = this.getClass().getDeclaredFields();
            Field fields_to_be_updated[] = obj.getClass().getDeclaredFields();

            for (Field f1 : updated_fields) {
//System.out.println(“New Values :”+f1.getName());
                for (Field f2 : fields_to_be_updated) {
                    if (f1.getName().equals(f2.getName()) && !f1.getName().equals("id")) {
//System.out.println(“Set”);
                        f2.set(obj, f1.get(this));
                    }
                }
            }
            session.update(obj);
            tx.commit();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public Object getById(Long id) {
        Object obj = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            obj = session.get(this.getClass(), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public List<? extends Object> getAll() {
        System.out.println("Abstract Entity GetAll()");
        List<? extends Object> list = null;
        try {
            System.out.println("93 : Abstract Entity");
            Session session = HibernateUtil.getSessionFactory().openSession();
            System.out.println("95 : Abstract Entity");
            Query query = session.createQuery("from " + this.getClass().getName());
            System.out.println("97 : Abstract Entity");
            list = query.list();
            System.out.println("99 : Abstract Entity");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
      public Long getByName(String name) {
        Long id = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            id = (Long)session.get(this.getClass(), name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }

    
    
}
